package com.hutou.chuangke.fragmentnet;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.hutou.chuangke.R;
import com.hutou.chuangke.activity.TrackDetailActivity;
import com.hutou.chuangke.fragment.AttachFragment;
import com.hutou.chuangke.widget.CircleImageView;
import com.hutou.chuangke.widget.DividerItemDecoration;
import com.hutou.chuangke.widget.MyGridview;
import com.hutou.chuangke.widget.SwipeRefreshLayout;

import java.util.ArrayList;

/**
 * Created by tyhdark on 2017/3/18.
 * 附近fragment
 */
public class NearbyFragment extends AttachFragment{
    private SwipeRefreshLayout swipeRefresh;
    private RecyclerView nearby_recyclerviewew;
    private ArrayList<String> mData = new ArrayList<>();
    private NearAdapter adapter;

    @Override
    public View onCreateView(LayoutInflater inflater,  ViewGroup container,  Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_nearby,null);
        return view;
    }

    @Override
    public void onViewCreated(View view,  Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        init(view);
        initData();
    }

    private void initData() {
        loadData();
    }

    private void init(View view) {
        nearby_recyclerviewew = (RecyclerView) view.findViewById(R.id.nearby_recyclerviewew);
        nearby_recyclerviewew.setLayoutManager(new LinearLayoutManager(getActivity()));
        nearby_recyclerviewew.addItemDecoration(new DividerItemDecoration(getContext(),DividerItemDecoration.VERTICAL_LIST));
        adapter = new NearAdapter();
        nearby_recyclerviewew.setAdapter(adapter);
        adapter.setonItemListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent intent = new Intent(getActivity(), TrackDetailActivity.class);
                startActivity(intent);
                Toast.makeText(getActivity(),"你点击了"+position,Toast.LENGTH_SHORT).show();
            }
        });
        swipeRefresh = (SwipeRefreshLayout) view.findViewById(R.id.swiperefresh);
        swipeRefresh.setHeaderView(crateHeadview());
        swipeRefresh.setFooterView(crateHeadview());
        swipeRefresh.setOnPullRefreshListener(new SwipeRefreshLayout.OnPullRefreshListener() {
            @Override
            public void onRefresh() {
                swipeRefresh.setRefreshing(true);
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        swipeRefresh.setRefreshing(false);
                    }
                },2000);
            }

            @Override
            public void onPullDistance(int distance) {}

            @Override
            public void onPullEnable(boolean enable) {}
        });

        swipeRefresh.setOnPushLoadMoreListener(new SwipeRefreshLayout.OnPushLoadMoreListener() {
            @Override
            public void onLoadMore() {
                swipeRefresh.setLoadMore(true);
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        swipeRefresh.setLoadMore(false);
                    }
                },2000);
            }

            @Override
            public void onPushDistance(int distance) {

            }

            @Override
            public void onPushEnable(boolean enable) {

            }
        });

    }

    private void loadData(){
        new AsyncTask<Void,Void,Integer>(){

            @Override
            protected Integer doInBackground(Void... voids) {
                int msg = 0;
                try{
                    for(int i = 0; i<10;i++){
                        mData.add("我是李大爷"+i);
                    }
                }catch(Exception e){
                    e.printStackTrace();
                    return 0;
                }
                return msg;
            }

            @Override
            protected void onPostExecute(Integer msg) {
                super.onPostExecute(msg);
                adapter.setData(mData);
                adapter.notifyDataSetChanged();
            }
        }.execute();

    }

    private View crateHeadview() {
        LayoutInflater inflater = LayoutInflater.from(getActivity());
        View view = inflater.inflate(R.layout.loading_circle,null);
        return view;
    }

    Handler handler = new Handler();

    public interface OnItemClickListener{
        void onItemClick(View view,int position);
    }

    class NearAdapter extends RecyclerView.Adapter<NearAdapter.ViewHolder>{
        private ArrayList<String> mData = new ArrayList<>();
        private OnItemClickListener mOnItemClickListener;
        public void setonItemListener(OnItemClickListener mOnItemClickListener){
            this.mOnItemClickListener = mOnItemClickListener;
        }
        @Override
        public NearAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            NearAdapter.ViewHolder holder = new NearAdapter.ViewHolder(LayoutInflater.from(getActivity())
                    .inflate(R.layout.item_nearby,null));
            return holder;
        }

        @Override
        public void onBindViewHolder(final NearAdapter.ViewHolder holder, int position) {
            holder.nametv.setText(mData.get(position));
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    int pos = holder.getLayoutPosition();
                    mOnItemClickListener.onItemClick(holder.itemView, pos);

                }
            });
            if(position==2) {
                holder.add_view_layout.addView(crateHeadview());
            }
        }

        public void setData(ArrayList<String> mData){
            this.mData = mData;
        }

        @Override
        public int getItemCount() {
            return mData.size();
        }

        class ViewHolder extends RecyclerView.ViewHolder{
            TextView nametv,datetv,contexttv,addresstv,commenttv,praise_usernametv;
            ImageView headiv,followiv,commentiv,shareiv;
            LinearLayout praise_layout;
            FrameLayout add_view_layout;
            MyGridview gridview;
            CheckBox praiseiv;
            public ViewHolder(View view) {
                super(view);
                nametv = (TextView) view.findViewById(R.id.nametv);//昵称
                datetv = (TextView) view.findViewById(R.id.datetv);//时间
                contexttv = (TextView) view.findViewById(R.id.contexttv);//发布内容
                addresstv = (TextView) view.findViewById(R.id.addresstv);//地址
                commenttv = (TextView) view.findViewById(R.id.commenttv);//评论人数
                praise_usernametv = (TextView) view.findViewById(R.id.praise_usernametv);//点赞人名
                headiv = (CircleImageView) view.findViewById(R.id.headiv);//头像
//                followiv = (ImageView) view.findViewById(R.id.followiv);//关注按钮
                commentiv = (ImageView) view.findViewById(R.id.commentiv);//评论按钮
                praiseiv = (CheckBox) view.findViewById(R.id.praiseiv);//点赞按钮
                shareiv = (ImageView) view.findViewById(R.id.shareiv);//分享按钮
                praise_layout = (LinearLayout) view.findViewById(R.id.praise_layout);//点赞人名布局
                add_view_layout = (FrameLayout) view.findViewById(R.id.add_view_layout);
                gridview = (MyGridview) view.findViewById(R.id.gridview);
            }
        }
    }
}
